package com.hainatong.jcmvvmbase;

import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;

import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.hainatong.jcmvvmbase.databinding.FragmentPageBinding;
import com.hainatong.jcmvvmbase.dbtoolroom.User;
import com.hainatong.mvvmbase.base.BaseMvvmFragment;
import com.hainatong.mvvmbase.rxbus.RxBus;
import com.hainatong.mvvmbase.rxbus.RxBusBaseMessage;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class FirstFragment extends BaseMvvmFragment<FragmentPageBinding,FirstViewModel>
{

    public static FirstFragment newInstance()
    {
        FirstFragment fragment = new FirstFragment();
        return fragment;
    }

    @Override
    public int getLayoutId()
    {
        return R.layout.fragment_page;
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState)
    {
        super.onViewCreated(view, savedInstanceState);
        viewModel.text.set("点击");
        viewDataBinding.setViewModel( viewModel );
        initView();
    }

    private void initView()
    {
        viewDataBinding.text2.setOnClickListener(view -> {

            showLoading();
            viewModel.getInfoByNet("FirstFragment")
                    .observe(getViewLifecycleOwner(),result ->{
                        viewModel.text.set( result );
                        showContent();
                    });

        });

        initRxBus();
    }

    private void initRxBus()
    {
        Disposable disposable = RxBus.getDefault().toObservable(Constants.INFO1 , RxBusBaseMessage.class)
                          .subscribe( msg -> {
                                  if( msg.getCode() == 100  )
                                  {
                                     User user = (User) msg.getObject();
                                     viewModel.text.set( user.getUsername() );
                                  }
                              });
        addSubscription( disposable );
    }


    @Override
    public int getBindingVariable()
    {
        return 0;
    }

    @Override
    protected void onRetryBtnClick()
    {

    }

}
